﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace TA.Turbo
{
	/// <summary>
	/// Contains some methods that extend <see cref="HttpContext"/>.
	/// </summary>
	public static class _HttpContextExtensions
	{
		internal const string RequestItemFilesKey = "TA.Turbo.FileUpload.Files",
			RequestItemOperKey = "TA.Turbo.FileUpload.Operation";
		/// <summary>
		/// Gets the list of files that were uploaded as part of this context's request.
		/// </summary>
		/// <param name="context"></param>
		/// <returns></returns>
		public static UploadedFile[] get_Files(this HttpContext context)
		{
			if (context.Items[RequestItemFilesKey] == null)
			{
				context.Items[RequestItemFilesKey] = new List<UploadedFile>();
			}
			return ((List<UploadedFile>)context.Items[RequestItemFilesKey]).ToArray();
		}
		/// <summary>
		/// Gets the <see cref="FileUploadOperation"/> which is processing this request.
		/// </summary>
		/// <param name="context"></param>
		/// <returns></returns>
		public static FileUploadOperation get_Operation(this HttpContext context)
		{
			return context.Items[RequestItemOperKey] as FileUploadOperation;
		}
		internal static void AddFile(this HttpContext context, UploadedFile file)
		{
			if (context.Items[RequestItemFilesKey] == null)
			{
				context.Items[RequestItemFilesKey] = new List<UploadedFile>();
			}
			((List<UploadedFile>)context.Items[RequestItemFilesKey]).Add(file);
		}
		internal static void SetOperation(this HttpContext context, FileUploadOperation operation)
		{
			context.Items[RequestItemOperKey] = operation;
		}
	}
}
